package com.dqyt.khjc.controller;

import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 个人信息管理
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询用户信息 和  所有角色权限
     *
     * @param request
     * @return
     */
    @RequestMapping("/api/getUserres")
    public ResultMsg getUserById(HttpServletRequest request) {
        ResultMsg resultMsg = userService.getUserById(request);
        return resultMsg;
    }

    /**
     * 修改个人信息
     *
     * @param request
     * @param avatar   头像
     * @param userName 姓名
     * @param phone    手机
     * @param account  账号
     * @param seal     印章
     * @return
     */
    @RequestMapping(value = "/api/updateUser", method = {RequestMethod.POST})
    public ResultMsg updateUser(HttpServletRequest request, String avatar, String userName, String phone, String account, String seal) {
        ResultMsg resultMsg = userService.updateUser(request, avatar, userName, account,phone, seal);
        return resultMsg;
    }

    /**
     * 修改手机号
     *
     * @param request
     * @param newPhone
     * @param oldPhone
     * @param code
     * @return
     */
    @RequestMapping(value = "/api/updatePhone", method = {RequestMethod.POST})
    public ResultMsg updatePhone(HttpServletRequest request, String newPhone, String oldPhone, String code) {
        ResultMsg resultMsg = userService.updatePhone(request, newPhone, oldPhone, code);
        return resultMsg;
    }


    /**
     * 修改密码
     *
     * @param request
     * @param oldPassword
     * @param newPassword
     * @param newPassword2
     * @return
     */
    @RequestMapping(value = "/api/updatePwd", method = {RequestMethod.POST})
    public ResultMsg updatePwd(HttpServletRequest request, String oldPassword, String newPassword, String newPassword2) {
        ResultMsg resultMsg = userService.updatePwd(request, oldPassword, newPassword, newPassword2);
        return resultMsg;
    }


    /**
     * 分页获取我的印章列表
     *
     * @param request
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/api/getMysealPage", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getMysealPage(HttpServletRequest request,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer size) {
        ResultMsg resultMsg = userService.getMysealPage(request, page, size);
        return resultMsg;
    }


    /**
     * 新建一个我的印章图片
     *
     * @param request
     * @param userId
     * @param imgstr
     * @return
     */
    @RequestMapping(value = "/api/addMysealPage", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg addMysealPage(HttpServletRequest request,
                                   @RequestParam(defaultValue = "") String userId,
                                   @RequestParam(defaultValue = "") String imgstr) {
        ResultMsg resultMsg = userService.addMysealPage(request, userId, imgstr);
        return resultMsg;
    }

    /**
     * 设置印章启用和禁用
     *
     * @param request
     * @param sealId
     * @return
     */
    @RequestMapping(value = "/api/setSealType", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg setSealType(HttpServletRequest request, long sealId) {
        ResultMsg resultMsg = userService.setSealType(request, sealId);
        return resultMsg;
    }


    /**
     * 删除签章
     *
     * @param request
     * @param sealId
     * @return
     */
    @RequestMapping(value = "/api/deleteSeal", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg deleteSeal(HttpServletRequest request, long sealId) {
        ResultMsg resultMsg = userService.deleteSeal(request, sealId);
        return resultMsg;
    }


    /**
     * 新增 用户
     *
     * @param request
     * @param userName
     * @param phone
     * @param account
     * @param department
     * @param role
     * @param password
     * @param password2
     * @return
     */
    @RequestMapping(value = "/api/addUser", method = {RequestMethod.POST})
    public ResultMsg addUser(HttpServletRequest request, String userName, String phone, String account, String department, String role, String password, String password2) {
        ResultMsg resultMsg = userService.addUser(request, userName, phone, account, department, role, password, password2);
        return resultMsg;
    }


    /**
     * 修改 用户
     *
     * @param request
     * @param userName
     * @param phone
     * @param account
     * @param department
     * @param role
     * @return
     */
    @RequestMapping(value = "/api/updateUserYl", method = {RequestMethod.POST})
    public ResultMsg updateUserYl(HttpServletRequest request, long userId, String userName, String phone, String account, String department, String role) {
        ResultMsg resultMsg = userService.updateUserYl(request, userId, userName, phone, account, department, role);
        return resultMsg;
    }


    /**
     * 分页查询用户列表
     *
     * @param request
     * @param page
     * @param size
     * @param userName
     * @param phone
     * @param account
     * @param role
     * @param statue
     * @return
     */
    @RequestMapping(value = "/api/getUserListPage", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserListPage(HttpServletRequest request,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     @RequestParam(name = "userName", required = false, defaultValue = "") String userName,
                                     @RequestParam(name = "phone", required = false, defaultValue = "") String phone,
                                     @RequestParam(name = "account", required = false, defaultValue = "") String account,
                                     @RequestParam(name = "role", required = false, defaultValue = "0") Integer role,
                                     @RequestParam(name = "statue", required = false, defaultValue = "-99") Integer statue) {

        ResultMsg resultMsg = userService.getUserListPage(request, page, size, userName, phone, account, role, statue);
        return resultMsg;
    }

    /**
     * 重置密码
     *
     * @param request
     * @param userId
     * @return
     */
    @RequestMapping(value = "/api/resetPassword", method = {RequestMethod.POST})
    public ResultMsg resetPassword(HttpServletRequest request, long userId, String msd, String userName) {
        ResultMsg resultMsg = userService.resetPassword(request, userId, msd, userName);
        return resultMsg;
    }


    /**
     * 登陆限制 用户信息
     *
     * @param userId 用户id
     * @param statue 1:允许登陆    2:限制登录
     * @return
     */
    @RequestMapping(value = "/api/loginYhglRes", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg loginYhglRes(HttpServletRequest request, long userId, int statue, String userName) {
        ResultMsg resultMsg = userService.loginYhglRes(request, userId, statue, userName);
        return resultMsg;
    }



    /**
     * 查询 维修员和检验员
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getUserByRoleWxOrJyy", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserByRoleWxOrJyy(HttpServletRequest request) {
        ResultMsg resultMsg = userService.getUserByRoleWxOrJyy(request);
        return resultMsg;
    }



    /**
     * 查询除去自己以外的 检验员
     *
     * @param request
     * @param role
     * @return
     */
    @RequestMapping(value = "/api/getUserByRoleExceptMe", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserByRoleExceptMe(HttpServletRequest request, String role) {
        ResultMsg resultMsg = userService.getUserByRoleExceptMe(request, role);
        return resultMsg;
    }



    /**
     * 查询某个角色的所有用户
     *
     * @param request
     * @param role
     * @return
     */
    @RequestMapping(value = "/api/getUserByRole2", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserByRole2(HttpServletRequest request, String role) {
        ResultMsg resultMsg = userService.getUserByRole2(request, role);
        return resultMsg;
    }




    /**
     * 查询所有用户
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getUserList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserList(HttpServletRequest request) {
        ResultMsg resultMsg = userService.getUserList(request);
        return resultMsg;
    }


    /**
     * 查询某个角色的用户  包含印章信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getUserListJc", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserListJc(HttpServletRequest request, String role) {
        ResultMsg resultMsg = userService.getUserListJc(request, role);
        return resultMsg;
    }






    /**
     * 查询某个角色的用户  包含印章信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/api/getUserListJcLHQ", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMsg getUserListJcLHQ(HttpServletRequest request, String role) {
        ResultMsg resultMsg = userService.getUserListJcLHQ(request, role);
        return resultMsg;
    }


}
